﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DzInfo.XH.FormPlugIn
{
    [Description("茂名新辉套打插件插件"), HotUpdate]
    public class NotePrint : AbstractDynamicFormPlugIn
    {
        public static string Head_Key = "FBillHead";
        public static string Head_Date = "FDate";

        public static string Entity_Key = "FEntity";
        public static string Entity_BillNo = "FBillNo";
        public static string Entity_Material = "FMATERIALNAME";
        public static string Entity_Qty = "FQty";

        private DataTable curShowTable;

        /// <summary>
        /// 套打显示逻辑
        /// </summary>
        /// <param name="e"></param>
        public override void OnPrepareNotePrintData(PreparePrintDataEventArgs e)
        {
            
            var did = e.DataSourceId;
            if (did == Head_Key)
            {
                //单据头字段
                var objType = e.DynamicObjectType;
                var billObj = new DynamicObject(objType);
                List<string> keyList = GetHedaList();
                foreach (string key in keyList)
                {
                    if (objType.Properties.ContainsKey(key))
                    {
                        billObj[key] = this.View.Model.GetValue(key);
                    }
                }
                e.DataObjects = new DynamicObject[] { billObj };
                return;
            }
            if (did == "FKHWLZ")
            {
                var objType = e.DynamicObjectType;
                List<string> keyList = GetList1();
                this.Model.ClearNoDataRow();
                DynamicObjectCollection rows = this.View.Model.DataObject["FKHWLZ"] as DynamicObjectCollection;

                if (rows.Count > 0)
                {
                    List<DynamicObject> printDatas = new List<DynamicObject>();
                    foreach (var row in rows)
                    {
                        var rowObj = new DynamicObject(objType);
                        foreach (string key in keyList)
                        {
                            var v = row[key];
                            if (objType.Properties.ContainsKey(key))
                            {
                                rowObj[key] = v;
                            }
                        }
                        printDatas.Add(rowObj);
                    }
                    e.DataObjects = printDatas.ToArray();
                }

                return;
            }
            if (did == "F_POSW_Entity_mpw")
            {
                var objType = e.DynamicObjectType;
                //var billObj = new DynamicObject(objType);
                //var dataobj = e.DataObjects;
                List<string> keyList = GetList2();
                this.Model.ClearNoDataRow();
                DynamicObjectCollection rows = this.View.Model.DataObject["POSW_K67fd152d"] as DynamicObjectCollection;

                if (rows.Count > 0)
                {
                    List<DynamicObject> printDatas = new List<DynamicObject>();
                    foreach (var row in rows)
                    {
                        var rowObj = new DynamicObject(objType);
                        foreach (string key in keyList)
                        {
                            var v = row[key];
                            if (objType.Properties.ContainsKey(key))
                            {
                                rowObj[key] = v;
                            }
                        }
                        printDatas.Add(rowObj);
                    }
                    e.DataObjects = printDatas.ToArray();
                }

                return;
            }
            if (did == "F_POSW_Entity_60c")
            {
                var objType = e.DynamicObjectType;
                List<string> keyList = GetList3();
                this.Model.ClearNoDataRow();
                DynamicObjectCollection rows = this.View.Model.DataObject["POSW_K8ad88f7c"] as DynamicObjectCollection;

                if (rows.Count > 0)
                {
                    List<DynamicObject> printDatas = new List<DynamicObject>();
                    foreach (var row in rows)
                    {
                        var rowObj = new DynamicObject(objType);
                        foreach (string key in keyList)
                        {
                            var v = row[key];
                            if (objType.Properties.ContainsKey(key))
                            {
                                rowObj[key] = v;
                            }
                        }
                        printDatas.Add(rowObj);
                    }
                    e.DataObjects = printDatas.ToArray();
                }

                return;
            }
            if (did == "F_POSW_Entity_vbc")
            {
                var objType = e.DynamicObjectType;
                List<string> keyList = GetList4();
                this.Model.ClearNoDataRow();
                DynamicObjectCollection rows = this.View.Model.DataObject["POSW_Kf53c3dac"] as DynamicObjectCollection;

                if (rows.Count > 0)
                {
                    List<DynamicObject> printDatas = new List<DynamicObject>();
                    foreach (var row in rows)
                    {
                        var rowObj = new DynamicObject(objType);
                        foreach (string key in keyList)
                        {
                            var v = row[key];
                            if (objType.Properties.ContainsKey(key))
                            {
                                rowObj[key] = v;
                            }
                        }
                        printDatas.Add(rowObj);
                    }
                    e.DataObjects = printDatas.ToArray();
                }

                return;
            }
            if (did == "F_POSW_Entity_6yb")
            {
                var objType = e.DynamicObjectType;
                List<string> keyList = GetList5();
                this.Model.ClearNoDataRow();
                DynamicObjectCollection rows = this.View.Model.DataObject["POSW_Kabda9edc"] as DynamicObjectCollection;

                if (rows.Count > 0)
                {
                    List<DynamicObject> printDatas = new List<DynamicObject>();
                    foreach (var row in rows)
                    {
                        var rowObj = new DynamicObject(objType);
                        foreach (string key in keyList)
                        {
                            var v = row[key];
                            if (objType.Properties.ContainsKey(key))
                            {
                                rowObj[key] = v;
                            }
                        }
                        printDatas.Add(rowObj);
                    }
                    e.DataObjects = printDatas.ToArray();
                }

                return;
            }
            //if (e.DataSourceId == Head_Key)
            //{   //单据头字段
            //    var objType = e.DynamicObjectType;
            //    var billObj = new DynamicObject(objType);
            //    List<string> keyList = new List<string>() { Head_Date };
            //    foreach (string key in keyList)
            //    {
            //        if (objType.Properties.ContainsKey(key))
            //        {
            //            billObj[key] = this.View.Model.GetValue(key);
            //        }
            //    }
            //    e.DataObjects = new DynamicObject[] { billObj };
            //    return;
            //}
            //if (e.DataSourceId == Entity_Key)
            //{
            //    if (curShowTable == null)
            //        return;
            //    //表体字段，根据显示数据填入
            //    var objType = e.DynamicObjectType;
            //    List<string> keyList = new List<string>() { Entity_BillNo, Entity_Material, Entity_Qty };
            //    List<DynamicObject> printDatas = new List<DynamicObject>();
            //    for (int i = 0; i < curShowTable.Rows.Count; ++i)
            //    {
            //        var rowObj = new DynamicObject(objType);
            //        var datatRow = curShowTable.Rows[i];
            //        foreach (string key in keyList)
            //        {
            //            if (objType.Properties.ContainsKey(key))
            //            {
            //                rowObj[key] = datatRow[key];
            //            }
            //        }
            //        printDatas.Add(rowObj);
            //    }
            //    e.DataObjects = printDatas.ToArray();
            //    return;
            //}
        }

        private List<string> GetList5()
        {
            List<string> keyList = new List<string>();
            keyList.Add("F_POSW_Text_ii9");
            keyList.Add("F_POSW_Decimal_rks");
            keyList.Add("F_POSW_Decimal_71r");
            keyList.Add("F_POSW_Decimal_f02");
            keyList.Add("F_POSW_Amount_15v");

            return keyList;
        }

        private List<string> GetList4()
        {
            List<string> keyList = new List<string>();
            keyList.Add("FPZBH");

            keyList.Add("F_POSW_Text_n6p");
            keyList.Add("F_POSW_Decimal_i6k");
            keyList.Add("F_POSW_Decimal_amv");
            keyList.Add("F_POSW_Decimal_7wd");
            keyList.Add("F_POSW_Amount_gmk");

            return keyList;
        }

        private List<string> GetList3()
        {
            List<string> keyList = new List<string>();
            keyList.Add("FSEQ2");
            keyList.Add("F_POSW_Date_e9h");
            keyList.Add("F_POSW_Text_zg7");
            keyList.Add("F_POSW_Text_9l8");
            keyList.Add("F_POSW_Base_7w7");
            keyList.Add("FDW");
            keyList.Add("F_POSW_Qty_xxu");

            keyList.Add("F_POSW_Decimal_ikt");
            keyList.Add("F_POSW_Price_xfa");
            keyList.Add("F_POSW_Decimal_f5d");
            keyList.Add("F_POSW_Price_vzn");
            keyList.Add("F_POSW_Amount_y10");
            keyList.Add("F_POSW_Amount_ito");
            keyList.Add("F_POSW_Amount_vxl");
            return keyList;
        }

        private List<string> GetList1()
        {
            List<string> keyList = new List<string>();
            keyList.Add("FCPLB");
            keyList.Add("FXSTD");
            keyList.Add("FXSY");
            keyList.Add("FNUMBER");
            keyList.Add("FNAME");
            keyList.Add("FQCYE");

            keyList.Add("FBQXL");
            keyList.Add("FBQQTXL");
            keyList.Add("FBQQTXE");
            keyList.Add("FBQXE");
            keyList.Add("FBQSKE");
            keyList.Add("FBQYE");
            keyList.Add("FDQZQK");
            return keyList;
        }

        private List<string> GetHedaList()
        {
            List<string> keyList = new List<string>();
            keyList.Add("FORG");
            keyList.Add("FSTADATE");
            keyList.Add("FENDDATE");
            keyList.Add("F_POSW_CheckBox_c6t");
            keyList.Add("F_POSW_Base_xvz");
            keyList.Add("FORG_FName");
            keyList.Add("FCustName");
            keyList.Add("FXSYNAME");
            return keyList;
        }

        private List<string> GetList2()
        {
            List<string> keyList = new List<string>();
            keyList.Add("FSEQ");
            keyList.Add("F_POSW_Date_1bv");
            keyList.Add("F_POSW_Text_0ne");
            keyList.Add("F_POSW_Text_uo3");
            keyList.Add("F_POSW_Amount_rec");
            keyList.Add("F_POSW_Amount_bpq");
            keyList.Add("F_POSW_Amount_d7o");
            keyList.Add("FCPQty");
            keyList.Add("FDZQty");
            keyList.Add("FYearMonth");
            return keyList;
        }
    }
}
